package com.globallogic.javatrainings.sergiiivanov.tests;

import static org.junit.Assert.*;

import org.junit.Test;

import com.globallogic.javatrainings.sergiiivanov.model.Const;
import com.globallogic.javatrainings.sergiiivanov.model.Course;
import com.globallogic.javatrainings.sergiiivanov.model.Student;
import com.globallogic.javatrainings.sergiiivanov.model.Teacher;
import com.globallogic.javatrainings.sergiiivanov.model.UniversityWarning;
import com.globallogic.javatrainings.sergiiivanov.resources.ErrorCodes;

public class TeacherTest extends BasicPersonTest{
    Teacher teacher = new Teacher();
    
    /**
     * <pre>
     * 39  6.    Teachers
     * 40  6.1.  Teacher has name, email, phone
     * 41  6.1.1.    all could be changed
     * ...
     * 43  6.2.  Teacher has resume
     * 44  6.2.1.    records could be added and deleted
     * </pre>
     * 
     * @throws UniversityWarning
     */
    public TeacherTest() throws UniversityWarning {
        super.person = teacher;
        super.BasicPersonSetup();
    }

    /**
     * <pre>
     * 42  6.1.2.    name should be unique within students of one university
     * </pre>
     * @throws Exception
     */
    @Test
    public void uniqueNameTest() throws Exception {
        u.teachers.addPerson(nPerson1, mobile, email);
        try {
            u.teachers.addPerson(nPerson1, mobile, email);
            fail("Missed " + ErrorCodes.PERSON_NAME_NOT_UNIQUE+nPerson1);
        } catch (Exception e) {
            assertEquals(ErrorCodes.PERSON_NAME_NOT_UNIQUE+nPerson1, e.getMessage());
        }
        u.teachers.addPerson(nPerson2, mobile, email);
        u.teachers.removeTeacher(u.teachers.getTeacherByIndex(0));
        u.teachers.addPerson(nPerson1, mobile, email);
    }

    /**
     * <pre>
     * 45  6.3.  Teacher could be assigned to course
     * 46  6.3.1.    any number of courses
     * 47  6.3.2.    could be unassigned at any time
     * </pre>
     * 
     */
    @Test
    public void assignUnassignCourseTest() throws Exception {
        u = getUniversityDummy();
        teacher = u.teachers.getTeacherByIndex(0);
        for(Course c: u.courses.getCourses().copy()){
            teacher.addCourse(c);
            assertEquals("teacher added course "+c.getName(), teacher, c.getTeacher());
        }
        assertArrayEquals(u.courses.getCourses().toArray(), teacher.getCourses().toArray());
        for(Course c: teacher.getCourses().copy()){
            teacher.removeCourse(c);
            assertNull("teacher removed course "+c.getName(), c.getTeacher());
        }
        assertArrayEquals(new Course[]{}, teacher.getCourses().toArray());
    }
    
    @Test
    public void setTeacherTest() throws Exception {
        u = getUniversityDummy();
        teacher = u.teachers.getTeacherByIndex(0);
        for(Course c: u.courses.getCourses().copy()){
            c.setTeacher(teacher);
            assertTrue("course "+c.getName()+" set teacher", teacher.getCourses().indexOf(c) >= 0);
        }
        assertArrayEquals(u.courses.getCourses().toArray(), teacher.getCourses().toArray());
        for(Course c: u.courses.getCourses().copy()){
            c.setTeacher(null);
            assertTrue("course "+c.getName()+" set teacher", teacher.getCourses().indexOf(c) < 0);
        }
        assertArrayEquals(new Course[]{}, teacher.getCourses().toArray());
    }
    
    @Test
    public void getTeachersListTest() throws Exception {
        u=getUniversityDummy();
        assertArrayEquals(
                new String[]{nPerson1,nPerson2,nPerson3},
                u.teachers.getTeachers().toNamesList()
                );
    }

    /**
     * <pre>
     * 49  6.5.  Teacher relevant scores table should be available
     * </pre>
     * 
     * @throws Exception
     */
    @Test
    public void getScoresTest() throws Exception {
        u = getUniversityDummy();
        Course c = u.courses.getCourseByIndex(0);
        teacher = u.teachers.getTeacherByIndex(0);
        c.setTeacher(teacher);
        Student s = u.students.getStudentByIndex(0);
        c.enrollStudent(s);
        s.addScore(c.getLectureByIndex(0), Const.MIN_SCORE, empty);
        s.addScore(c.getLectureByIndex(0), Const.MAX_SCORE, empty);
        String[][] scoresExp = {
                { c.getName(),
                        c.getLectureByIndex(0).getName(),
                        Integer.toString(Const.MIN_SCORE),
                        empty,
                        teacher.getName() },
                { c.getName(),
                        c.getLectureByIndex(0).getName(),
                        Integer.toString(Const.MAX_SCORE),
                        empty,
                        teacher.getName() }
        };
        assertArrayEquals(scoresExp, teacher.getScoreTable());
    }
}
